package com.example.smartall.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.diaglog.ChangInfoDialog;
import com.example.smartall.ui.UserInfoActivity;
import com.example.smartall.utils.Constant;
import com.example.smartall.utils.SPUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserInfoListAdapter extends BaseAdapter {

    private Context context;
    //    private Map<String,String> map;
    private List<String> key;
    private List<String> value;
//    public UserInfoListAdapter(Context context, Map<String, String> map) {
//        this.context = context;
//        this.map = map;
//    }


    public UserInfoListAdapter(Context context, List<String> value) {
        this.context = context;
        this.value = value;
        key = Arrays.asList(context.getResources().getStringArray(R.array.UserInfoList).clone());
    }

    @Override
    public int getCount() {
//        return map == null ? 0 : map.size();
        return key.size();
    }

    @Override
    public Object getItem(int i) {
//        return map.get(i);
        return value.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (i == 0) {
            view = null;
            ViewHolder viewHolder;
            if (view == null) {
                viewHolder = new ViewHolder();
                view = LayoutInflater.from(context).inflate(R.layout.mine_list_item2, null, false);
                viewHolder.tv = (TextView) view.findViewById(R.id.tv);
                viewHolder.Itag = (ImageView) view.findViewById(R.id.Itag);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.tv.setText(key.get(i));
            Glide.with(context).load(ApiConfig.BASE_URL + value.get(i)).into(viewHolder.Itag);
        } else {
            view = null;
            ViewHolder viewHolder;
            if (view == null) {
                viewHolder = new ViewHolder();
                view = LayoutInflater.from(context).inflate(R.layout.mine_list_item, null, false);
                viewHolder.tv = (TextView) view.findViewById(R.id.tv);
                viewHolder.tag = (TextView) view.findViewById(R.id.tag);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.tv.setText(key.get(i));
            //身份证中间数字隐藏
            if (value.get(i).length() == 18) {
                StringBuilder builder = new StringBuilder(value.get(i));
                builder = builder.replace(2, 14, "*************");
                viewHolder.tag.setText(builder);
            } else {
                viewHolder.tag.setText(value.get(i));
            }

        }
//        view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                ChangInfoDialog dialog = new ChangInfoDialog(context);
//                dialog.setListener(new ChangInfoDialog.OnWhichClickListener() {
//                    @Override
//                    public void onOkClick(String s) {
//
//                        HashMap<String,Object> params = new HashMap<>();
//                        params.put(Constant.NICK_NAME,new SPUtils(context,Constant.SP_USER_FILE_NAME).getString(Constant.USER_NICKNAME));
//                        Api.config(ApiConfig.CHANGE_USER_URL,params).putRequest(new NetCallBack() {
//                            @Override
//                            public void onSuccess(String res) {
//                                Log.e("TAG",res);
//                                Collections.replaceAll(value,value.get(i),s);
//                                UserInfoListAdapter.this.notifyDataSetChanged();
//                            }
//
//                            @Override
//                            public void onFailure(Exception e) {
//                                Log.e("TAG",e.toString());
//                            }
//                        },new SPUtils(context,Constant.SP_USER_FILE_NAME).getString(Constant.TOKEN));
//                    }
//                });
//                dialog.show();
//
//            }
//        });
        return view;
    }

    class ViewHolder {
        private TextView tv;
        private TextView tag;
        private ImageView Itag;
    }
}
